<?php
include_once "common/basic.php";
$func->validateLogin();
if($_POST["publish"]){
	$uid = $_SESSION[uid];
	$desc = util::substr(trim($_POST[desc]),0,140);
	$tags = trim($_POST[tags]);
	$client_ip = $_SERVER[REMOTE_ADDR];

	$feed_id = $func->save_feed_resource($uid,$desc,$tags,$client_ip);
	
	$result = true;
	$res_count = 0;
	if($_POST[lx]=="torrent"){
		if($_FILES[btfile] && count($_FILES[btfile])>0 && $_POST[btname] && count($_POST[btname])>0){
			for($i=0;$i<count($_POST[btname]);$i++){
				$file = array(
					'name'=>$_FILES[btfile][name][$i],
					'type'=>'torrent',
					'tmp_name'=>$_FILES[btfile][tmp_name][$i],
					'size'=>$_FILES[btfile][size][$i],
					'error'=>$_FILES[btfile][error][$i]
				);
				if($file[size]>1024*1024){
					$func->error("e12062601","上传的.torrent文件不得大于1MB");
				}
				$name  =trim($_POST[btname][$i]);
				if($file && $name){
					$fid = $func->saveFile($_SESSION[uid], $file);
					if($fid){
						$result = $result && $func->save_feed_resource_torrent($feed_id,$name,$fid,$file[size]);
						if($result)$res_count=$res_count+1;
					}else{
						$result = false;
					}
				}
			}
		}else{
			$result = false;
		}
	}elseif($_POST[lx]=="ed2k"){
		if($_POST[ed2k_link] && count($_POST[ed2k_link])>0){
			for($i=0;$i<count($_POST[ed2k_link]);$i++){
				$ed2k_link = trim($_POST[ed2k_link][$i]);
				$ed2k_name =  trim($_POST[ed2k_name][$i]);
				$result = $result && $func->save_feed_resource_link($feed_id,'ed2k',$ed2k_name,$ed2k_link);
				if($result)$res_count=$res_count+1;
			}
		}else{
			$result = false;
		}
	}elseif($_POST[lx]=="magnet"){
		if($_POST[magnet_link] && count($_POST[magnet_link])>0){
			for($i=0;$i<count($_POST[magnet_link]);$i++){
				$magnet_link =  trim($_POST[magnet_link][$i]);
				$magnet_name =  trim($_POST[magnet_name][$i]);
				$result = $result && $func->save_feed_resource_link($feed_id,'magnet',$magnet_name,$magnet_link);
				if($result)$res_count=$res_count+1;
			}
		}else{
			$result = false;
		}
	}elseif($_POST[lx]=="thunder"){
		if($_POST[thunder_link] && count($_POST[thunder_link])>0){
			for($i=0;$i<count($_POST[thunder_link]);$i++){
				$thunder_link =  trim($_POST[thunder_link][$i]);
				$thunder_name =  trim($_POST[thunder_name][$i]);
				$result = $result && $func->save_feed_resource_link($feed_id,'thunder',$thunder_name,$thunder_link);
				if($result)$res_count=$res_count+1;
			}
		}else{
			$result = false;
		}
	}
	
	if(!$result || $res_count==0){
		$func->feed_delete($uid,$feed_id);
		$func->error("e12062701","没有资源发布！");
	}
	
	//save pics
	if($_FILES[pic] && count($_FILES[pic])>0){
		for($i=0;$i<count($_FILES[pic][name]);$i++){
			$file = array(
				'name'=>$_FILES[pic][name][$i],
				'type'=>'pic',
				'tmp_name'=>$_FILES[pic][tmp_name][$i],
				'size'=>$_FILES[pic][size][$i],
				'error'=>$_FILES[pic][error][$i]
			);
			if(($file[size]/1024)>5*1024 || $file[size]==0){
				continue;
			}
			if($file){
				$fid = $func->saveFile($_SESSION[uid], $file);
				if($fid){
					$filepath = $func->getFilePathByFID($fid);
					$tmpfilepath = $func->tmpfilepath();
					$res = $func->image_resize($filepath, null, 150, $tmpfilepath, "h");
					$fid_thumb = "";
					if($res){
						$file_thumb = array();
						$file_thumb[name]=$file[name];
						$file_thumb[type]=$file[type];
						$file_thumb[tmp_name]=$tmpfilepath;
						$file_thumb[size]=filesize($tmpfilepath);
						$file_thumb[error]="";
						$fid_thumb = $func->saveFile($_SESSION[uid], $file_thumb);
						unlink($tmpfilepath);
					}
					$result = $result && $func->save_feed_resource_pic($feed_id,$file[name],$fid,$file[size],$fid_thumb);
				}else{
					$result = false;
					break;
				}
			}
		}
	}
	
	if($result){
		if($_POST[publish_tencent]){
			$func->oauth_tencent_publish($uid,$feed_id);
		}
		$func->redirect("my.php");
	}else{
		$func->feed_delete($uid, $feed_id);
		$func->error("e12062002","系统忙，请稍后再试！");//发布资源出错
	}
}else{
	$oauth_tencent_info = $func->oauth_tencent_info_get($_SESSION[uid]);
	$tl->set_file("publish");
	$tl->p();
}
?>